<?php

namespace App\Http\Controllers;

use App\Comment;
use App\Post;
use App\Zan;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class PostController extends Controller
{
    /**
     * @todo 文章列表页
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(){
        $posts = Post::orderBy('created_at','desc')->withCount(['comments','zans'])->paginate(config('home.per_page',10));
        //dd($posts);
        return view('post.index',compact('posts'));
    }

    /**
     * @todo 展示文章详情
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function show(Post $post){
        $post->load('comments');
        return view('post.show',compact('post'));
    }

    /**
     * @todo 添加文章
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create(){
        return view('post.create');
    }

    /**
     * @todo 保存创建文章
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function store(Request $request){
        //验证
        $this->validate($request,[
            'title' => 'required|string|max:100|min:5',
            'content' => 'required|string|min:10'
        ]);
        if (!Auth::check()){
            return redirect()->back()->withErrors('请先登录再保存文章');
        }
        $user_id = Auth::id();
        $data = array_merge(request(['title','content']),compact('user_id'));
        //保存
        $post = Post::create($data);
        //响应
        return redirect('/posts/'.$post->id);
    }

    /**
     * @todo富文本编辑器图片上传方法
     * @param Request $request
     * @return string
     */
    public function upload(Request $request){
        $file = $request->file('wangEditorH5File');
        $path = $file->storePubliclyAs('postimg/'.date('Ymd'),time().'.'.$file->extension());
        return asset('storage/'.$path);
    }

    /**
     * @todo 编辑文章
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit(Post $post){
        return view('post.edit',compact('post'));
    }

    /**
     * @todo 更新文章
     * @param Post $post
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function update(Post $post,Request $request){
        //TODO::用户权限验证
        $this->authorize('update',$post);
        //验证
        $this->validate($request,[
            'title' => 'required|string|max:100|min:5',
            'content' => 'required|string|min:10'
        ]);
        //保存
        $post->title = $request->input('title');
        $post->content = $request->input('content');
        $post->save();
        //渲染
        return redirect("/posts/{$post->id}");
    }

    /**
     * @todo 删除文章
     * @param Post $post
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function delete(Post $post){
        //TODO::用户权限验证
        $this->authorize('delete',$post);
        $post->delete();
        return redirect('/posts');
    }

    public function comment(Post $post){

        //1.验证
        if (!Auth::check()){
            return back()->withErrors('请先登录才能评论文章');
        }
        $this->validate(request(),[
           'content'=>'required|min:3'
        ]);
        $comment = new Comment();
        $comment->user_id = Auth::id();
        $comment->content = request('content');
        $post->comments()->save($comment);
        //渲染
        return back();
    }

    public function zan(Post $post){

        $param = [
            'user_id' => Auth::id(),
            'post_id' => $post->id,
        ];
        //有就查出来，没有就创建
        Zan::firstOrCreate($param);
        return back();
    }

    public function unzan(Post $post){
        $post->zan(Auth::id())->delete();
        return back();
    }

    public function search(){
        //1.验证
        $this->validate(request(),[
           'query'=>'required'
        ]);
        $query = request('query');
        $posts = Post::search($query)->orderBy('created_at','desc')->paginate(config('home.per_page',10));
        //2.逻辑
        return view('post.search',compact('posts','query'));
    }
}
